package com.work.demo.file.sort;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Objects;

/**
 * 文件信息
 *
 * @author Adrian
 * @date 2020/12/9 10:12
 */
public class SmallFileInfo {

    /**
     * 改行value是否使用
     */
    private Boolean valueIsUse;

    /**
     * 当前行号
     */
    private Long lineNum;

    /**
     * 当前一行值
     */
    private String value;

    /**
     * 文件绝对路径
     */
    private String fileAbsolutePath;

    /**
     * BufferedReader引用
     */
    private BufferedReader reader;

    public String readNextValue(){
        //判断上一轮读取的值是否使用
        if (this.valueIsUse) {
            try {
                String line = null;
                if ((line = this.reader.readLine()) != null) {
                    this.value = line;
//                    System.out.println("当前读取行数为：" + this.lineNum + ",value为：" + this.value);
                    this.lineNum++;
                    this.valueIsUse = false;
                    return this.value;
                }
                //读完了
                else {
                    return null;
                }
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
        //否则返回上一轮读取的值
        else {
            return this.value;
        }
    }

    public Boolean getValueIsUse() {
        return valueIsUse;
    }

    public void setValueIsUse(Boolean valueIsUse) {
        this.valueIsUse = valueIsUse;
    }

    public Long getLineNum() {
        return lineNum;
    }

    public void setLineNum(Long lineNum) {
        this.lineNum = lineNum;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getFileAbsolutePath() {
        return fileAbsolutePath;
    }

    public void setFileAbsolutePath(String fileAbsolutePath) {
        this.fileAbsolutePath = fileAbsolutePath;
    }

    public BufferedReader getReader() {
        return reader;
    }

    public void setReader(BufferedReader reader) {
        this.reader = reader;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || this.getClass() != obj.getClass()) {
            return false;
        }
        SmallFileInfo fileInfo = (SmallFileInfo) obj;
        return this.fileAbsolutePath.equals(fileInfo.getFileAbsolutePath());
    }

    @Override
    public int hashCode() {
        return Objects.hash(this.fileAbsolutePath);
    }
}

